import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@WebServlet(name = "ServletCt")
//ServletCt 名字不要跟ServletContext重名
public class ServletCt extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
  /*  public void init(ServletConfig servletConfig) throws ServletException {
        ServletContext servletContext= servletConfig.getServletContext();
    }*/
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 测试servletContext 对象(代表当前 Web 应用的对象)的使用  几乎不用servletContext了
     /*   在 Servlet 中，调用 ServletContext 接口的 setAttribute()
        方法可以创建一些属性，这些属性被存放在 ServletContext 对象中。
        应用中所有 Servlet 都可以对这些属性进行访问和操作，
        通过它们可以实现应用内不同 Servlet 之间的数据通讯。*/

        ServletContext servletContext = this. getServletContext();
        String name = "无法";
        servletContext.setAttribute("name",name);

        response.setContentType("text/html");
        // 编码问题
        response.setCharacterEncoding("utf-8");


        //  使用 servletContext 对象获取静态配置文件jbdc
        // InputStream流
        InputStream is =  this. getServletContext().getResourceAsStream("/WEB-INF/classes/jdbc.properties");
        Properties properties = new Properties();
        properties.load(is);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        System.out.println(user+password);// 获取成功
    }


}
